package de.fmannan.wildit.ui;

import java.text.ParseException;
import java.util.Collection;

import android.app.ListActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.Toast;
import de.fmannan.wildit.domainmodel.Alarm;
import de.fmannan.wildit.domainmodel.AlarmType;
import de.fmannan.wildit.persistence.PersistenceProvider;

public abstract class AlarmSettings extends ListActivity
{
    private static final String TAG = "AlarmSettings";
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
	super.onCreate(savedInstanceState);

	setContentView(R.layout.alarm_list);
	populateList();
	registerForContextMenu(getListView());

    }

    private void populateList()
    {
	// Gather all alarms belonging to the current dialog and add them to our
	// view
	Collection<Alarm> alarms;
	try
	{
	    alarms = PersistenceProvider.getPersistenceProvider(
	    	this).getAlarms(getTypeFilter());
		ArrayAdapter<Alarm> alarmAdapter = new ArrayAdapter<Alarm>(this,
			R.layout.alarm_list_row);
		for (Alarm alarm : alarms)
		{
		    alarmAdapter.add(alarm);
		}
		setListAdapter(alarmAdapter);
	} 
	catch (ParseException e)
	{
	    Log.e(TAG, e.getMessage(), e);
	    // TODO Show error message to user and export to string resources
	    Toast.makeText(this, "Error while trying to display the list", Toast.LENGTH_SHORT);
	}
    }

    protected abstract AlarmType getTypeFilter();
}
